
package com.csc440.team2.database.entities;

import java.io.Serializable;
import java.util.List;


import android.content.Context;


import com.csc440.team2.database.DBAdapter;


public class Message implements Serializable
{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long id;
    private String message;
    private FolderType folder;
    private boolean read;
    private String sender;


    public Message()
    {
        setId(-1);
        setMessage("");
        setFolder(FolderType.SENT);
        setRead(false);
        setSender("");
    }


    public Message(long id)
    {
        setId(id);
    }


    public Message(String message, FolderType folder, boolean read, String sender)
    {
        setId(-1);
        setMessage(message);
        setFolder(folder);
        setRead(read);
        setSender(sender);
    }


    public Message(long id, String message, FolderType folder, boolean read, String sender)
    {
        setId(id);
        setMessage(message);
        setFolder(folder);
        setRead(read);
        setSender(sender);
    }


    public long getId()
    {
        return id;
    }


    public void setId(long id)
    {
        this.id = id;
    }


    public String getMessage()
    {
        return message;
    }


    public void setMessage(String message)
    {
        this.message = message;
    }


    public FolderType getFolder()
    {
        return folder;
    }


    public void setFolder(FolderType folder)
    {
        this.folder = folder;
    }


    public boolean isRead()
    {
        return read;
    }


    public void setRead(boolean read)
    {
        this.read = read;
    }


    public String getSender()
    {
        return sender;
    }


    public void setSender(String sender)
    {
        this.sender = sender;
    }


    private static DBAdapter getDatabase(Context context)
    {
        DBAdapter adapter = new DBAdapter(context);
        adapter.open();
        return adapter;
    }


    public boolean load(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getMessage(this);
        adapter.close();
        return result;
    }


    public boolean save(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.saveMessage(this);
        adapter.close();
        return result;
    }


    public boolean delete(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.deleteMessage(this);
        adapter.close();
        return result;
    }


    public static List<Message> getAll(Context context)
    {
        List<Message> result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getAllMessages();
        adapter.close();
        return result;
    }
}
